package com.uchedao.vehicledetection.view.carlist.fragment;

import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.uchedao.mybaselibrary.activity.BaseFragment;
import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.view.carlist.Entity.CarSeries;
import com.uchedao.vehicledetection.view.carlist.adapter.SeriesAdapter;
import com.uchedao.vehicledetection.view.carlist.db.CarDbHelper;

import java.util.ArrayList;

/**
 * Created by ZhangZhanghua on 2015/9/14 0014.
 */
public class FragmentSeries extends BaseFragment {
    private ListView seriesListView;
    private ArrayList<CarSeries> seriesList;
    private SeriesAdapter adapter;
    private String brandName;
    private int brandId;
    private ISeriesClickListener listener;
    @Override
    protected String getActivityTag() {
        return "FragmentSeries";
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_series;
    }

    @Override
    protected void initView() {
        seriesListView = (ListView) findViewById(R.id.series);

    }

    @Override
    protected void initData() {
        brandId =  getArguments().getInt("carBrandId");
//        brandId = intent.getExtras().getInt("carBrandId");
//        brandName = intent.getExtras().getString("carBrandName");
        seriesList = CarDbHelper.getCarSeries(getActivity(), brandId);
        adapter = new SeriesAdapter(getActivity() , seriesList);
        seriesListView.setAdapter(adapter);
    }

    @Override
    protected void initListener() {

        seriesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                intent.putExtra("carBrandId", brandId);
//                intent.putExtra("carBrandName", brandName);
                int sId = ((CarSeries) adapter.getItem(position)).getId();
                String sName = ((CarSeries) adapter.getItem(position)).getName();
                if (sId == -1){
                    getActivity().finish();
                }
                listener.onSeriesClick(sId, sName);
            }
        });
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try{
            listener =(ISeriesClickListener)activity;
        }catch(ClassCastException e){
            throw new ClassCastException(activity.toString()+"must implement OnArticleSelectedListener");
        }
    }

    public interface ISeriesClickListener
    {
        void onSeriesClick(int sId ,String sName);
    }
}
